/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.insta.browser.container;

/**
 * Permet de créer des services partager facilement.
 *
 * @author Maarek
 * @param <T> Le type de l'objet partager
 * @version $Id: $Id
 */
public class Share<T> implements F<T> {

    Object container;
    F1<T, Object> callback;

    /**
     * <p>Constructor for Share.</p>
     *
     * @param container Le container qui sera passé en parametre à la callback
     * @param callback La callback qui créer le service. Prend en paramêtre le
     *  container
     */
    public Share(Object container, F1<T, Object> callback) {
        this.container = container;
        this.callback = callback;
    }

    protected T shared = null;

    /** {@inheritDoc} */
    @Override
    public T call() {
        if (this.shared == null) {
            this.shared = this.callback.call(this.container);
        }
        return this.shared;
    }

    /**
     * <p>isAlreadyAllocated.</p>
     *
     * @return Vrai si le service à déjà été alloué.
     */
    public boolean isAlreadyAllocated() {
        return this.shared != null;
    }

}
